// 94. 二叉树的中序遍历

/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

// 中序遍历 左 根 右
function inorderTraversal(root: TreeNode | null): number[] {
  if (!root) return [];

  const start: TreeNode[] = [];
  const result: number[] = [];
  // start.push(root)

  while (start.length || root) {
    // const node: TreeNode = start.pop()
    if (root) {
      start.push(root);
      root = root.left;
    } else {
      const node: TreeNode = start.pop();
      result.push(node.val);
      root = node.right;
    }
  }

  return result;
}
